/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.tabs.results;

import javax.swing.JDialog;
import systole.domain.persons.Patient;
import systole.domain.report.Report;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.view.abstractController.AbstractEditViewController;
import systole.view.tabs.controllers.ReportModel;
import systole.view.utils.ErrorDialog;

/**
 *
 * @author jmj
 */
public abstract class ResultDialogController extends AbstractEditViewController {

    protected FacadeDB facadeDB;
    protected JDialog form;
    protected ReportModel reportModel;
    protected Patient patient;

    public ResultDialogController() {
        super();
        this.facadeDB = FacadeDB.getInstance();
    }

    public void ok() {
        this.loadReportFromForm();
        this.form.setVisible(false);
        this.form.dispose();
    }

    public void cancel() {
        this.form.setVisible(false);
        this.form.dispose();
    }

    protected abstract void initFormsControls() throws ExceptionDAO;

    protected abstract JDialog createForm(java.awt.Frame parent);

    protected abstract void loadIconOnForm();

    protected abstract void loadReportOnForm(Report report);

    protected abstract void loadReportFromForm();

    protected void showForm(java.awt.Frame parent, Report report) {
        try {
            this.form = this.createForm(parent);
            this.initFormsControls();
            this.loadReportOnForm(report);
            this.form.setLocationRelativeTo(parent);
            this.setCloseListener(this.form);
            this.loadIconOnForm();
            this.form.setVisible(true);
        } catch (ExceptionDAO ex) {
            FacadeDB.getInstance().refreshSession();
            ErrorDialog.showError(this.form, ex.getMessage());
        }
    }

    /**
     * 
     * @param parent
     * @param model
     * @param patient
     */
    public void edit(java.awt.Frame parent, ReportModel model, Patient patient) {
        this.reportModel = model;
        this.patient = patient;
        this.showForm(parent, model.getCurrentReport());
    }
}
